<template>
    <!-- main开始   start -->
    <div class="main">
        <div class="top_main w clearfix">
            <h3>填写并核对订单信息</h3>
            <div class="top_bootom">
                <div class="top_adress">
                    <h4>收件人信息</h4>
                    <div class="smail_adress" @mouseleave="AddressLeave()">
                        <div class="adress" 
                            :class="{backColor:Index==index}" 
                            v-for="(address,index) in Address" 
                            :key="address.id" 
                            @click="GetAdress(index,address)"
                            @mouseenter="AddressNenter(index)"
                            
                            >
                            <span class="adress_left">{{address.name}}</span>
                            <p>
                                <i>{{address.address}}</i>
                                 <i>{{address.phone}}</i>
                                <span>默认地址</span>
                            </p>
                        </div>
                    </div>
                </div>
                <div class="top_adress top_pay">
                    <h4>支付方式</h4>
                    <div class="pay_bootom">
                        <span class="adress_left">在线支付</span>
                        <span class="adress_left pay_right">货到付款</span>
                    </div>
                </div>
                <div class="top_adress way">
                    <h4>送货清单</h4>
                    <div class="big_way">
                        <div class="top_way">
                            <h4>配送方式</h4>
                            <div class="smail_bootom">
                                <span>天天快递</span>
                                <p>配送时间：预计8月10日（周三）09:00-15:00送达</p>
                            </div>
                        </div>
                        <div class="bottom_way" v-if="detailArrayList">
                            <h4>商品清单</h4>
                            <div class="smail_bootom" v-for="(list,index) in detailArrayList" :key="index">
                                <div class="bottom_first">
                                    <img :src="list.imgUrl" alt="" width="82px" height="82px">
                                    <p>
                                        <span>{{list.skuName}}</span>
                                        <i>7天无理由退货</i>
                                    </p>
                                    <span class="color">￥{{list.orderPrice}}.00</span>
                                    <i>X{{list.skuNum}}</i>
                                    <strong>有货</strong>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="bbs">
                    <h4>买家留言：</h4>
                    <textarea name="" id="" cols="30" rows="10" placeholder="建议留言前先与商家沟通确认" v-model="textarea"></textarea>
                </div>
                <div class="bill">
                    <h4>发票信息：</h4>
                    <p>普通发票（电子）个人 明细</p>
                    <h4>使用优惠/抵用</h4>
                </div>
            </div>
            <div class="money">
                <ul>
                    <li>
                        <b>
                            <i>{{detailArrayList.length}}</i>
                            件商品，总商品金额
                        </b>
                        <span>¥{{getPrice}}.00</span>
                    </li>
                    <li>
                        <b>返现：</b>
                        <span>0.00</span>
                    </li>
                    <li>
                        <b>运费：</b>
                        <span>0.00</span>
                    </li>
                    <li v-if="textarea">
                        <b>留言：</b>
                        <span>{{textarea}}</span>
                    </li>
                </ul>
            </div>
            <div class="trade">
                <div class="price">
                    应付金额:
                    <span>¥{{getPrice}}.00</span>
                </div>
                <p v-if="minAddress.address">寄送至: {{minAddress.address}}  收货人：{{minAddress.name}} {{minAddress.phone}}</p>
            </div>
            <button @click="Submit">提交订单</button>
        </div>
    </div>
    <!-- main结束   end -->
</template>

<script>
export default {
    name:'trade',
    data() {
        return {
            detailArrayList:[],   //定义了一个变量获取trade数据
            Address:[],
            Index:-1,
            minAddress:{},
            textarea:'',
            getList:{},
            fleg:true
        }
    },
    mounted() {
        this.GeTrade();
        this.Get_Adress();
    },
    methods: {
        
        //发送请求给tarde接口
         async GeTrade(){
             let trade = await this.$API.Auth_Trade()
            if (trade.data.code==200) {
                this.detailArrayList = trade.data.data.detailArrayList
                this.getList = trade.data.data
            }
        },

        //获取地址信息
       async Get_Adress(){
            let adress = await this.$API.Get_Tarde();
            if (adress.data.data.code==200) {
                this.Address = adress.data.data.data
            }
        },

        //点击收件信息时  把fleg等于false 表示取消移入事件
        GetAdress(getindex,address){
            this.Index = getindex   //点击时当前显示样式
            this.minAddress = address   //把地址信息 赋值给minAddress
            this.fleg = false   //点击了 则把移入事件取消
        },

        //移入时收件信息时 启动样式 
        AddressNenter(index){
            if (this.fleg) {
                this.Index = index   //移入时显示样式
            }
        },

        //移出大div时 则清除样式  如果为没有点击则收件信息 则样式清除   
        // 如果点击了 则当用户移出大div时  则把fleg等于true  这样则可以继续显示样式
        AddressLeave(){
            if (this.fleg) {
               this.Index = -1   //清除样式
            }else{
                this.fleg = true
            }   
        },

        //点击提交  发送请求
       async Submit(){
           this.$route.meta.fleg=true
        //    获取所有需要发送的数据
            let tradeNo = this.getList.tradeNo
            let List = {
                consignee:this.minAddress.name,
                consigneeTel:this.minAddress.phone,
                deliveryAddress:this.minAddress.address,
                paymentWay:"ONLINE",
                orderComment:this.textarea,
                orderDetailList:this.detailArrayList
            }
            // 如果姓名与地址 有值则可以发送请求
            if (List.consignee && List.deliveryAddress ) {
                let Orde =  await this.$API.submitOrde(List,tradeNo);
                if (Orde.data.code == 200) {
                    this.$bus.orde = Orde.data.data
                    this.$router.push('/pay') 
                }else{
                    alert(Orde.data.message)
                }
            }else{
                alert('请选择地址')
            }
        }
    },
    computed: {
        // 获取总价格
        getPrice(){
           return this.detailArrayList.reduce((num,b)=>{
               return num + (b.orderPrice * b.skuNum)
           },0)
        }
    },
}
</script>

<style scoped lang='less'>
.main{
    .top_main{
        h3{
            color: #000;
        }
        h4{
            color: #000;
        }
        strong{
            font-weight: normal;
        }
        .top_bootom{
            border: 1px solid #dddddd;
            margin-top: 10px;
            box-sizing: border-box;
            padding: 25px;
            .adress_left{
                margin-left: 20px;
                color: #000;
                line-height: 30px;
                display: block;
                width: 100px;
                height: 30px;
                text-align: center;
                border: 1px solid #dddddd;
            }
            .top_adress{
                border-bottom: 1px solid #0f0c0c;
                .smail_adress{
                    margin-bottom: 20px; 
                    .adress{
                        display: flex;
                        // width: 100%;
                        width: 650px;
                        margin-top: 20px;
                        line-height: 30px;
                        cursor: pointer;
                        p{
                            width: 540px;
                            margin-left: 20px;
                            span{
                                width: 56px;
                                height: 24px;
                                line-height: 23px;
                                margin-left: 10px;
                                background-color: #878787;
                                color: #fff;
                                text-align: center;
                                display: inline-block;
                            }
                        }
                    }
                    // &>div:hover{
                    //     .adress_left{
                    //         border: 1px solid red;
                    //         background: url(./imges/choosed.png) no-repeat 88px 16px;
                    //     }
                    //     p{
                    //          cursor: pointer;
                    //         background-color: #dddddd;
                    //     }
                    // }

                    .backColor{
                        .adress_left{
                            border: 1px solid red;
                            background: url(./imges/choosed.png) no-repeat 88px 16px;
                        }
                        p{
                             cursor: pointer;
                            background-color: #dddddd;
                        }
                    }

                }
            }
            .top_pay{
                margin-top: 30px;
                .pay_bootom{
                    display: flex;
                    margin-top: 20px;
                    &>span:hover{
                          border: 1px solid red;
                            background: url(./imges/choosed.png) no-repeat 88px 16px;
                    }
                    span{
                        cursor: pointer;
                    }
                    .pay_right{
                        background: none;
                        border: 1px solid #dddddd;
                        margin-left: 10px;
                    }
                }   
                padding-bottom: 20px;
            }
            .way{
                margin-top: 30px;
                border: 0;
                .big_way{
                    margin-left: 5px;
                    margin-top: 30px;
                    .top_way{
                        width: 1080px;
                        height: 90px;
                        background: #f4f4f4;
                        padding: 15px;
                        margin: 0 auto;
                        .smail_bootom{
                            margin-top: 35px;
                            display: flex;
                            line-height: 30px;
                            span{
                                display: inline-block;
                                border: 1px solid #ddd;
                                width: 120px;
                                height: 30px;
                                line-height: 30px;
                                text-align: center
                            }
                            p{
                                margin-left: 20px;
                            }
                        }
                        
                    }
                    .bottom_way{
                        width: 1080px;
                        background: #feedef;
                        padding: 15px;
                        margin: 0 auto;
                        margin-top: 5px;
                        .smail_bootom{
                            margin-top: 10px;
                            .bottom_first{
                                position: relative;
                                display: flex;
                                line-height: 30px;
                                justify-content: space-between;
                                p{
                                    position: relative;
                                    width: 610px;
                                    i{
                                        position: absolute;
                                        bottom: 0;
                                        left: 0;
                                        color: #c81623;
                                    }
                                }
                                .color{
                                    color: #e12228;
                                    font-weight: 700;
                                    font-size: 14px;
                                }
                                i{
                                    // border: 1px solid yellow;
                                    // margin-right: 10px;
                                }
                                strong{
                                    margin-right: 90px;
                                }
                            }
                        }
                    }
                }
            }
            .bbs{
                padding: 15px 0px;
                border-bottom: 1px solid #dddddd;
                textarea{
                    margin-top: 20px;
                    width: 100%;
                    border-color: #e4e2e2;
                    line-height: 1.8;
                    outline: none;
                    height: 45px;
                    font-size: 11px;
                    resize: none;
                }
            }
            .bill{
                margin-top: 15px;
                p{
                    margin-left: 15px;
                    line-height: 50px;
                    color: #333;
                }
            }
        }
        .money{
            // float: right;
            display: flex;
            justify-content: flex-end;
            margin-top: 20px;
            ul{
                li{
                    display: flex;
                    justify-content: space-between;
                    width: 220px;
                    line-height: 30px;
                }
            }
            b{
                font-weight: normal;
                i{
                    color: red;
                }
            }
            color: #333;
        }
        .trade{
            margin-top: 20px;
            padding: 10px;
            line-height: 30px;
            box-sizing: border-box;
            text-align: right;
            background-color: #f4f4f4;
            border: 1px solid #ddd;
            .price{
                span{
                    margin-left: 5px;
                    color: #e12228;
                    font-weight: 700;
                    font-size: 14px;
                }
            }
        }
        button{
            float: right;
            width: 164px;
            height: 56px;
            text-align: center;
            color: rgb(255, 255, 255);
            background-color: rgb(225, 37, 27);
            outline: none;
            border: 0;
            cursor: pointer;
            margin-top: 10px;
            font-size: 15px;
        }
    }
}
</style>